var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function orderList(data) {
    dataSource.splice(0, dataSource.length);//清空数组
    if(data.list.length>0){
        $('.dataTables_wrapper').show();
        $('.dataTables_filter').show();
        $('#productList').show();
        $('.tableul').show();
    }else{
        $('.dataTables_wrapper').hide();
        $('.dataTables_filter').hide();
        $('#productList').hide();
        $('.tableul').hide();
    }
    $.each(data.list, function (i, field) {
        var rowData = new Array();
        rowData.push(field);
        dataSource.push(rowData);
    });
    initDataTable(dataSource);
    function initDataTable(dataSource) {
        var dttable = $('#productList').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#productList").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "render":function (data, type, row) {
                        var html = '';
                        if(data.imgUrl){
                            html+='<p class="p" spu_id="'+data.spu_id+'"><img class="img" src="'+data.imgUrl+'"/></p>'
                        }else{
                            html+='<p class="p" spu_id="'+data.spu_id+'"><img class="img" src=""/></p>';
                        }
                        html+='<p class="detail p" sku_code = "'+data.sku_code+'">'+data.sku_name+data.sale_attri_value+'</p>';
                        return html;
                    },
                    "targets": 0
                }
            ]
        });
    }
}


$('#exportSubChannelConfigInfo').attr('href',root.dockingplatform.downloadSkuExcelTemplate);


function upLoadFile(val) {
    sessionStorage.setItem("val",val);
    $("#fileUp").trigger('click');
}

function ExportSkuPool(){

    var customerCode11 = sessionStorage.getItem("customerId");//渠道商id

    // };
    $('#putout').attr('href',root.dockingplatform.exportChannelSkuLogExcel
        + '?customerId=' + '' + customerCode11+ '&skuCode=' + '' + $('#skuCode').val()+ '&curPage=' + '' + 1+ '&rows=' + '' + 50)
}


function putDataToPage() {
    /*渲染spu sku值到页面*/
    var formData = new FormData();
    formData.append('file', $("#fileUp")[0].files[0]);
    formData.append("operateUserName",JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name);
    formData.append("customerId",sessionStorage.getItem("customerId"));
    formData.append("type",sessionStorage.getItem("val"));
    // console.log($("#fileUp")[0].value)
    $.ajax({
        url: root.dockingplatform.channelSkuByExcel,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        data: formData,
        success: function (data) {
            console.log(data);
            if(data.errorCode==0){
                $.Huimodalalert("导入成功",2000);
                setTimeout(function () {
                    location.replace(location.href);
                }, 3000);
            }else {
                layer.confirm(data.msg, function(index){
                        layer.close(index);
                        location.replace(location.href);
                });

            }

        }
    });
}
sku_cha();
function sku_cha() {
    console.log("costmerCode===",sessionStorage.getItem("customerId"));

    $("#channelName1").html(sessionStorage.getItem("customerName"));
    $("#channelId1").html(sessionStorage.getItem("customerId"));


    $.ajax({
        contentType: 'application/json',
        //url: root.goodsInter.goods_getSkuList,
        url: root.dockingplatform.selectChannelSkuFromExcel,
        cache: false,  //禁用缓存
        type: "POST",
        data: JSON.stringify({
            "curPage":1,
            "rows":20,
            "customerId":sessionStorage.getItem("customerId"),
            "skuCode":""
            //"parentCustomerCode":"11.11.111111",
            //"customerName":sessionStorage.getItem("customerName")
        }),
        success: function (data) {
            //sessionStorage.setItem("CRMCode",data.data.result.parentCustomerCode);
            skuList(data);
            var totalPage=""
            if(data !=null && data.data!=""){
                totalPage=data.data.totalPage
            }else {
                totalPage=0
            }
            $('.M-box4').pagination({
                pageCount: totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function(api) {
                    console.log(api.getCurrent());
                    $.ajax({
                        contentType: 'application/json',
                        //url: root.goodsInter.goods_getSkuList,
                        url: root.dockingplatform.selectChannelSkuFromExcel,
                        cache: false,  //禁用缓存
                        type: "POST",
                        data: JSON.stringify({
                            "curPage":api.getCurrent(),
                            "rows":20,
                            "customerId":sessionStorage.getItem("customerId"),
                            "skuCode":""
                        }),
                        success: function (data) {
                            skuList(data);
                            $("#spu_all").attr("checked",false);
                        }
                    });
                }
            });
        }
    });
    /*}*/

}

var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];

function skuList(data) {

    dataSource.splice(0, dataSource.length);//清空数组
    if(data.data !=null && data.data != '') {
        $.each(data.data.result, function (i, field) {
            var rowData = new Array();
            rowData.push(field.skuCode); //我司客户id
            rowData.push(field.skuName); //客户代码
            rowData.push(field.type); //客户名称
            rowData.push(field.operateReason);  //项目名称
            rowData.push(field.operateUsername);  //项目编码
            rowData.push(field.lastModifiedDate);  //项目名称
            dataSource.push(rowData);
        });
    }
    initDataTable(dataSource);

    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "order": [[1, "esc"]],
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": false,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    /*渠道*/
                    "render": function (data, type, row, meta) {
                        //var val = '<span><input class="skuId" type="checkbox" name="skuId" value = "'+ row[7] +'">Code码：'+row[8]+'</span>';
                        var val = '<span>' + row[0] + '</span>';
                        return val;
                    },
                    "targets": 0
                },
                {
                    /*我司客户ID*/
                    "render": function (data, type, row, meta) {
                        if (row[1] == null) {
                            row[1] = '';
                        }
                        // if (row[12] == null){
                        //     row[12] = '';
                        // }
                        /*var val = '<span>批发价：'+row[2]+'</span><br><span>零售价：'+row[9]+'</span>';*/
                        //var val = '<span>' +row[12]+ '&nbsp' +row[11]+ ' </span>';
                        var val = '<span>' + row[1] + ' </span>';
                        return val;
                    },
                    "targets": 1
                },

                {
                    /*CRM代码*/
                    "render": function (data, type, row, meta) {
                        if (row[2] == null) {
                            row[2] = '';
                        }
                        if(row[2]==0){
                            var value = "新增"
                        }else {
                            var value = "删除"
                        }
                        var val = '<span>' + value + '</span>';
                        return val;
                    },
                    "targets": 2
                },
                {
                    /*客户名称*/
                    "render": function (data, type, row, meta) {
                        if (row[3] == null) {
                            row[3] = '';
                        }
                        var val = '<span>' + row[3] + '</span>';
                        return val;
                    },
                    "targets": 3
                },
                {
                    /*对方项目编码*/
                    "render": function (data, type, row, meta) {
                        if (row[4] == null) {
                            row[4] = '';
                        }

                        var val = '<span>' + row[4] + '</span>';
                        return val;
                    },
                    "targets": 4
                },
                {
                    /*对方项目编码*/
                    "render": function (data, type, row, meta) {
                        if (row[5] == null) {
                            row[5] = '';
                        }

                        var val = '<span>' + row[5] + '</span>';
                        return val;
                    },
                    "targets": 5
                }
            ],
        });
        //权限控制函数
        // power()

    }

}

// 商品搜索匹配
$("#proCodeSearch").Validform({
    tiptype:2,
    callback:function(form){
        getListByCode(1)
        return false;
    }
});
//商品列表  商品名称
function getListByCode(page_num){

    $.ajax({
        contentType: 'application/json',
        //url: root.goodsInter.goods_getSkuList,
        url: root.dockingplatform.selectChannelSkuFromExcel,
        cache: false,  //禁用缓存
        type: "POST",
        data: JSON.stringify({
            "curPage":1,
            "rows":20,
            "customerId":sessionStorage.getItem("customerId"),
            "skuCode":$('#skuCode').val()
            //"parentCustomerCode":"11.11.111111",
            //"customerName":sessionStorage.getItem("customerName")
        }),
        success: function (data) {
            //sessionStorage.setItem("CRMCode",data.data.result.parentCustomerCode);
            skuList(data);
            totalPage = data.data.totalPage;
            $('.M-box4').pagination({
                pageCount: totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function(api) {
                    console.log(api.getCurrent());
                    $.ajax({
                        contentType: 'application/json',
                        //url: root.goodsInter.goods_getSkuList,
                        url: root.dockingplatform.selectChannelSkuFromExcel,
                        cache: false,  //禁用缓存
                        type: "POST",
                        data: JSON.stringify({
                            "curPage":api.getCurrent(),
                            "rows":20,
                            "customerId":sessionStorage.getItem("customerId"),
                            "skuCode":$('#skuCode').val()
                        }),
                        success: function (data) {
                            skuList(data);
                            $("#spu_all").attr("checked",false);
                        }
                    });
                }
            });
        }
    });



};
